package cn.edu.cxcyzx.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.cxcyzx.entity.Members;
import cn.edu.cxcyzx.entity.Project;
import cn.edu.cxcyzx.entity.Student;
import cn.edu.cxcyzx.service.MembersService;
import cn.edu.cxcyzx.supervisor.DaoFit;

@Service("MembersService")
public class MembersServiceImpl implements MembersService {
	
	@Resource
	public DaoFit daoFit;

	@Transactional
	@Override
	public void insertMembers(List<Members> list,String gitee_proName,String gitee_full_name) {
		// TODO Auto-generated method stub
		
		daoFit.getMembersDao().insertMembers(list);
		int num=daoFit.getMembersDao().getMemberNum_groupId(list.get(0).getGroupId());
		daoFit.getGroupDao().update_memberNum(num,list.get(0).getGroupId());
		daoFit.getProjectDao().update_memberNum(num,list.get(0).getGroupId());
		
		//更新房间人数
		Project pro=daoFit.getProjectDao().getProBy_groupId(list.get(0).getGroupId());
		/**
		 * 更新project表的gitee信息
		 */
		daoFit.getProjectDao().updateGitee(pro.getProjectId(), gitee_proName, gitee_full_name);
		int memberNum=daoFit.getApplyProjectDao().getProject_byProjectId(pro.getProjectId()).getMemberNum();
		if(num<memberNum){
			//项目小组实际的成员数量小于申报的数量
			daoFit.getRoomDao().addRest(pro.getRoomId(), memberNum-num);
		}
		
	}

	@Override
	public int getMemberNum_groupId(String groupId) {
		// TODO Auto-generated method stub
		return daoFit.getMembersDao().getMemberNum_groupId(groupId);
	}
	
	@Transactional
	@Override
	public void delete_oneMember(String groupId, String stuId) {
		// TODO Auto-generated method stub
		System.out.println("删除学生:groupId:"+groupId+",stuId:"+stuId);
		daoFit.getMembersDao().deleteMember(groupId, stuId);
		//改变成员数量
		daoFit.getProjectDao().minus_OneMember(groupId);
		daoFit.getGroupDao().minus_OneMember(groupId);
		System.out.println("删除成功");
	}
	


}
